<?php
/*
   Copyright 2012 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 require_once('config.php');

?><!doctype html><html>
<head>
 <?php include('./include/header.inc'); ?>
 <title>Sorting / <?php echo $sad_domain; ?></title>

</head>
<body>

<?php
 if (strlen($_POST['table'])>0 and preg_match('_^sort$_i',$_POST['action'])){

  foreach (array_keys($_POST) as $key){
   if (!preg_match('_^rec([0-9]+)$_i',$key,$a)){continue;}
   mysql_query('update `'.sad_safe_mysql($sad_prefix.$_POST['table']).
               '` set `i`="'.(int)$_POST[$key].'" where `id`='.$a[1]);
  }
  echo '<script>window.close();</script>You can safely close this window.</body></html>';
  exit;
 }

 if (strlen($_GET['table'])==0){
  echo '<script>window.close();</script>No you can not do this!.</body></html>';
  exit;
 }

 $tt=$_GET['title'];
 if (strlen($tt)<1){$tt='title';}

 $q=mysql_query('select `id`,`'.sad_safe_mysql($tt).'` as `title` from `'.
             sad_safe_mysql($sad_prefix.$_GET['table']).
             '` where `parent` like "'.sad_safe_mysql($_GET['parent']).
             '" order by `i` asc');
 if (mysql_error()){echo 'There is mysql error. Please check `parent`, `table`, `title` fields ';}else{
  $ms_t=32;
?><style>
div.record{
 width: 350px;
 border: 1px black solid;
 margin-left: 2px;
 padding: 3px;
 position: absolute;
 
 -moz-border-radius: 3px;
 -webkit-border-radius: 3px;
 border-radius: 3px;
 background-color: white;
 
 cursor: move;
}
div.record a{
 
}
</style><script>
var i_s  = new Array();
var id_s = new Array();
<?php
 echo 'var ms_t = '.$ms_t.";\r\n";
 $records=array();$i=1;$form='';
 while ($q_c=mysql_fetch_assoc($q)){
  echo "i_s[$i]  = {$q_c['id']};\r\n";
  echo "id_s[{$q_c['id']}] = $i;\r\n";
  $form.='<input type="hidden" id="rec'.$q_c['id'].'" name="rec'.$q_c['id'].'" value="'.$i.'">'."\r\n";

  $records[$q_c['id']]=array();
  $records[$q_c['id']]['id']   =$q_c['id'];
  $records[$q_c['id']]['i']    =$i;$i++;
  $records[$q_c['id']]['title']=$q_c['title'];
 }
 echo 'var max_i = '.($i-1).";\r\n";
 unset($q,$q_c,$i);
?>
</script>
<script src="<?php echo $sad_baseurl; ?>/i/style/sort.js"></script>
<script>
 $(document).mouseup(obj_up);
</script>

<?php
  foreach ($records as $q_c){
   echo '<div id="div'.$q_c['id'].'" class="record" style="top: '.
        ($ms_t*($q_c['i']-1)).'px;" onmousedown="obj_down(this,event);"
		onmousemove="obj_move(this,event);" >';
   echo '<a href="javascript:" onclick="rec_up('.$q_c['id'].');"><img src="'.
        $sad_baseurl.'/i/style/arrow_up.png" alt=" up " style="border: none"></a>';
   echo '<a href="javascript:" onclick="rec_down('.$q_c['id'].');"><img src="'.
        $sad_baseurl.'/i/style/arrow_down.png" alt=" down " style="border: none"></a>';// */
   echo htmlspecialchars($q_c['title']);
   if (strlen($_GET['edit'])>0){
    echo '<a href="'.$sad_baseurl.'/i/edit.php?table='.
         urlencode($_GET['table']).'&amp;id='.$q_c['id'].'&amp;edit='.
         urlencode($_GET['edit']).'" onclick="edit(this);" target="_blank">'.
         '<img src="'.$sad_baseurl.'/i/style/receipt--pencil.png" alt=" edit " '.
         'style="border: none"></a>';
   }
   echo '<a target="_blank" href="'.$sad_baseurl.'/i/delete.php?table='.
        urlencode($_GET['table']).'&amp;id='.$q_c['id'].'"><img src="'.
        $sad_baseurl.'/i/style/cross.png" alt=" delete " title="Delete="></a>';
   echo "</div>\r\n\r\n";
  }
  echo '<form method="post">'.$form.'<input type="hidden" name="action" '.
       'value="sort"><input type="hidden" name="table" value="'.
       htmlspecialchars($_GET['table']).'"><input type="submit" value="Sort" '.
       'style="position: absolute; top: '.($ms_t*$q_c['i']).'px"></form>';

 }//mysql_error

?>



</body></html>